package com.jgp.sys.controller;

import com.jgp.common.pojo.LabelValue;
import com.jgp.common.utils.JGPUtil;
import com.jgp.mico.client.MicroClient;
import com.jgp.mico.client.UriVariableStyle;
import com.jgp.sys.props.AttrPropBean;
import com.jgp.sys.repository.JGPRepository;
import com.jgp.sys.service.AttributeService;
import com.jgp.sys.ui.Result;
import com.jgp.sys.utils.SpringContextUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 项目   sys
 * 作者   loufei
 * 时间   2018/3/7
 */
@CrossOrigin
@Controller
@RequestMapping("/")
public class SysController extends JGPBackController {
    
    @Autowired
    private AttrPropBean propBean;
    
    @Autowired
    private AttributeService attributeService;
    
    @RequestMapping("/")
    public String root(){
        return "forward:/sys/main";
    }
    
    @RequestMapping("/sys/main")
    public String main(Model model){
        model.addAttribute("securityEnable", securityEnable);
        return "framework/main";
    }
    
    @RequestMapping("/sys/config-drop")
    @ResponseBody
    public Result drop(String activeKey,String model,String modelKey){
        if(StringUtils.isNotBlank(model)&&StringUtils.isNotBlank(modelKey)){
            JGPRepository jgpRepository = null;
            String [] arr = model.split(":");
            String repositoryName = arr[0].replace(".model.", ".repository.").replace(".secmodel.", ".secrepository.") + "Repository";
            try {
                jgpRepository = (JGPRepository) SpringContextUtil.getBean(Class.forName(repositoryName));
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            Object bean = jgpRepository.read(modelKey);
            List<LabelValue> list = new ArrayList<>();
            LabelValue labelValue = new LabelValue();
            labelValue.setValue(modelKey);
            labelValue.setLabel(JGPUtil.toMap(bean).get(arr[1])+"");
            list.add(labelValue);
            if(Objects.nonNull(list)) return ajaxReData("ldata", list);
        }else if(StringUtils.isNotBlank(activeKey)){
            if (ifInvokeFromRemote(propBean)) {
                MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
                LinkedHashMap<String,Object> uriVariables = new LinkedHashMap<>();
                uriVariables.put("activeKey",activeKey);
                return microClient.post(propBean.getMicroAppName(), "/sys/config-drop", Result.class, UriVariableStyle.KEY_VALUE_STYLE,uriVariables);
            }
            List<LabelValue> list = attributeService.queryLabelValuesByActiveKey(activeKey);
            if(Objects.nonNull(list)) return ajaxReData("ldata", list);
        }
        return ajaxRe(false);
    }
    
    @RequestMapping("/sys/config-drop-all")
    @ResponseBody
    public Result dropAll(@RequestBody List<String> activeKeys){
        if (ifInvokeFromRemote(propBean)) {
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            return microClient.postJson(propBean.getMicroAppName(), "/sys/config-drop-all", Result.class, activeKeys);
        }
        Map<String,List<LabelValue>> map = attributeService.queryLabelValuesByActiveKey(activeKeys);
        if(Objects.nonNull(map)) return ajaxReData("dropMap",map);
        return ajaxRe(false);
    }

/*

    @RequestMapping("index")
    public String index(Model model){
        model.addOAttribute("globalConfig", configService.globalConfig());

        return "main";
    }
*/

}
